<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 组织架构的内容 头部 -->
      <el-card class="tree-card">
        <!-- 放置结构内容 头部 -->
        <TreeTools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <!-- 放置一个<el-tree></el-tree> -->
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 传入内容 会循环多测 几个节点就循环几次 -->
          <!-- 作用域插槽 slot-scope="obj" 接受传递给插槽的数据 data 就是每个节点的数据对象-->
          <!-- 定义自定义方法 方便更新数据 -->
          <TreeTools
            slot-scope="{ data }"
            :tree-node="data"
            @addDepts="addDepts"
            @delDepts="getDepartments"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
    </div>
    <AddDept
      ref="addDept"
      :show-dialog.sync="showDialog"
      :tree-node="node"
      @addDepts="getDepartments"
    />
  </div>
</template>

<script>
import TreeTools from './compoents/tree-tools.vue'
import AddDept from './compoents/adddept.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
export default {
  components: {
    TreeTools, AddDept
  },
  data() {
    return {
      company: { name: '', manager: '' },
      departs: [
        {
          name: '总裁办',
          manager: '曹操',
          children: [
            {
              name: '董事会',
              manager: '刘备'
            }
          ]
        },
        {
          name: '行政部',
          manager: '孙权'
        },
        {
          name: '人事部',
          manager: '司马懿'
        }
      ],
      defaultProps: {
        label: 'name', // 表示从这个属性显示内容
        children: 'children' // 表示从这个属性去找子节点
      },
      showDialog: false, // 控制弹层显示
      node: null
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments()
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      this.departs = tranListToTreeData(result.depts, '')
      console.log(this.departs)
    },
    addDepts(node) {
      this.showDialog = true // 显示弹层
      // 因为node是当前点击的部门,此时应该将这个部门记录下来
      this.node = node
    },
    editDepts(node) {
      this.showDialog = true // 弹出层
      this.node = node
      // 应该在这里调用获取部门详情的方法
      // console.log(this.$refs.addDept)
      this.$refs.addDept.getDepartDetail(node.id)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
